﻿//********************************************************************************//
//创建日期：<创建日期，2016-04-26>
//创建作者：<郭登杰，guodengjie@shanghai3h.com>
//功能说明：三同时管理
//********************************************************************************//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BLL;
using BLL.Access;
using System.Data;
using Platform.Http;
using Web.Class;

namespace Web.Pages.YongShui.SanTongSGL
{
    public partial class SanTongSGL : BasePage
    {
        #region 属性

        /// <summary>
        /// 记录查询结果集
        /// </summary>
        private DataTable dtSource
        {
            get
            {
                return ViewState["dtSource"] as DataTable;
            }
            set
            {
                ViewState["dtSource"] = value;
            }
        }

        /// <summary>
        ///当前操作项ID
        /// </summary>
        private int SANTONGSID
        {
            get
            {
                return Convert.ToInt32(ViewState["SANTONGSID"]);
            }
            set
            {
                ViewState["SANTONGSID"] = value;
            }
        }

        #endregion

        #region 变量

        /// <summary>
        /// 水平衡管理实体对象
        /// </summary>
        YS_STS_SANTONGSGL _YS_STS_SANTONGSGL = new YS_STS_SANTONGSGL();
        
        /// <summary>
        /// 水平衡管理BLL对象
        /// </summary>
        YS_STS_SANTONGSGLBLL _YS_STS_SANTONGSGLBLL = new YS_STS_SANTONGSGLBLL();

        /// <summary>
        /// 数据访问操作类
        /// </summary>
        SpHelper _spHelper = new SpHelper();

        /// <summary>
        /// 用户信息对象
        /// </summary>
        YS_YG_YONGHUXX _YS_YG_YONGHUXX = new YS_YG_YONGHUXX();

        /// <summary>
        /// 用户信息BLL
        /// </summary>
        YS_YG_YONGHUXXBLL _YS_YG_YONGHUXXBLL = new YS_YG_YONGHUXXBLL();

        /// <summary>
        /// DataTable的行集合
        /// </summary>
        DataRowCollection drc;

        /// <summary>
        /// 操作日志BLL
        /// </summary>
        SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();

        #endregion

        #region 事件

        /// <summary>
        /// 页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack)
            {
                InitControl();
                Display();
            }
            else
            {
                this.gvSANTONGSHI.DataSource=this.dtSource;
                this.gvSANTONGSHI.DataBind();
            }
        }

        /// <summary>
        /// 绑定空表头
        /// </summary>
        protected override void Render(HtmlTextWriter sender)
        {
            base.Render(sender); 
        }

        /// <summary>
        /// grid添加
        /// </summary>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            //初始化数据
            InitData();
            this.Status = GridViewStatus.Add;
            Display();

        }

        /// <summary>
        /// grid放弃
        /// </summary>
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            this.Status = GridViewStatus.Init;            
            Display();
        }

        /// <summary>
        /// grid修改
        /// </summary>
        protected void lnbModify_Command(object sender, CommandEventArgs e)
        {
            this.SANTONGSID = Convert.ToInt32(e.CommandArgument);
            this.Status = GridViewStatus.Update;
            Display();
            SetData();
        }

        /// <summary>
        /// grid删除
        /// </summary>
        protected void lnbDel_Command(object sender, CommandEventArgs e)
        {
            //获取当前要操作对象
            this.SANTONGSID = Convert.ToInt32(e.CommandArgument);
            this._YS_STS_SANTONGSGL = _YS_STS_SANTONGSGLBLL.SelectYS_STS_SANTONGSGL(this.SANTONGSID,null);
            this._YS_STS_SANTONGSGL.S_CAOZUOR = LoginInfo.CurrentUser.Account;
            this._YS_STS_SANTONGSGL.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();
            this._YS_STS_SANTONGSGL.I_JLZT = -1;
            _SYS_LOGBLL.Log<YS_STS_SANTONGSGL>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "三同时管理", OperateState.Delete, _YS_STS_SANTONGSGL, null);
            if (_YS_STS_SANTONGSGLBLL.InsertOrUpdate(_YS_STS_SANTONGSGL, null))
            {
                
                LoadData(true);
            }
            PageTools.AlertMsg(this, "操作成功！");           
        }

        /// <summary>
        /// grid保存
        /// </summary>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (ValidateValue())
            {
                SaveData();
            }
        }

        /// <summary>
        /// 查询按钮事件
        /// </summary>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            LoadData(true);
            InitData();
        }

        /// <summary>
        /// 用户号回车事件
        /// </summary>
        protected void txtuserNoButton_Click(object sender, EventArgs e)
        {
            LoadData1(true);
        }

        #endregion

        #region 方法

        /// <summary>
        /// 初始化控件
        /// </summary>
        private void InitControl()
        {
            
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitData()
        {
            this.txtChaZhi.Text = "";
            this.txtDiZhi.Text = "";
            this.txtPINGGUDW.Text = "";
            this.txtPingGuSL.Text = "";
            this.txtRQ.Text = "";
            this.txtSHEJISL.Text = "";
            this.txtYonghuh.Text = "";
            this.txtYonghum.Text = "";
            this.txtYuShui.Text = "";
            this.txtZHONGSHUI.Text = "";
            this.cbJianGuan.Checked = false;
            this.txtRQ.Value = DateTime.Now;
        }

        /// <summary>
        /// 数据加载
        /// </summary>
        private void LoadData(bool  reGet)
        {
            if (reGet)
            {
                this.dtSource = _spHelper.P_YS_GETSANTONGS(this.contxtYongHuH.Text.Trim() == "" ? null : this.contxtYongHuH.Text.Trim(), this.contxtYongHuM.Text.Trim() == "" ? null : this.contxtYongHuM.Text.Trim(), this.contxtDiZhi.Text.Trim() == "" ? null : this.contxtDiZhi.Text.Trim(), null, null).Tables[0];            
            }
            this.gvSANTONGSHI.DataSource = this.dtSource;
            this.gvSANTONGSHI.DataBind();
            
        }

        /// <summary>
        ///用于控制添加、修改、查看三种状态的显示
        /// </summary>
        private void Display()
        {
            switch (this.Status)
            {
                case GridViewStatus.Init:
                    btnAdd.Visible = true;
                    GridViewContentTable.Visible = false;
                    this.gvSANTONGSHI.Enabled = true;
                    this.btnSearch.Enabled = true;
                    break;
                case GridViewStatus.Search:
                    btnAdd.Visible = true;
                    GridViewContentTable.Visible = true;
                    break;
                case GridViewStatus.Update:
                    btnAdd.Visible = false;
                    GridViewContentTable.Visible = true;
                    this.gvSANTONGSHI.Enabled = false;
                    this.txtYonghuh.Enabled = false;
                    this.lblOperateTip.Text = "修改";
                    this.btnSearch.Enabled = false;
                    break;
                case GridViewStatus.Add:
                    btnAdd.Visible = false;
                    GridViewContentTable.Visible = true;
                    this.gvSANTONGSHI.Enabled = false;
                    this.lblOperateTip.Text = "添加";
                    this.txtYonghuh.Enabled = true;
                    this.btnSearch.Enabled = false;
                    break;
            }
        }
      
        /// <summary>
        /// 给输入框赋值
        /// </summary>
        private void SetData()
        {
            //根据主键获取水平衡管理对象
            _YS_STS_SANTONGSGL = _YS_STS_SANTONGSGLBLL.SelectYS_STS_SANTONGSGL(this.SANTONGSID, null);
            //获取用户信息对象
            drc=new YS_YG_YONGHUXXBLL().SelectYS_YG_YONGHUXXesDynamic("i_jlzt=0 and i_leixing=0 and s_yonghuh='"+ _YS_STS_SANTONGSGL.S_YONGHUH+"'",null).Rows;
            this.txtYonghuh.Text = _YS_STS_SANTONGSGL.S_YONGHUH;
            this.txtYonghum.Text = drc[0]["S_YONGHUM"].ToString();
            this.txtDiZhi.Text = drc[0]["S_DIZHI"].ToString();
            //加载对象信息
            this.txtChaZhi.Text = this._YS_STS_SANTONGSGL.I_CHAZHI.ToString();
            this.txtPINGGUDW.Text = this._YS_STS_SANTONGSGL.S_PINGGUDW;
            this.txtPingGuSL.Text = this._YS_STS_SANTONGSGL.I_PINGGUSL.ToString();
            this.txtRQ.Value = this._YS_STS_SANTONGSGL.D_RIQI;
            this.txtSHEJISL.Text = this._YS_STS_SANTONGSGL.I_SHEJISL.ToString();
            this.txtYuShui.Text = this._YS_STS_SANTONGSGL.I_YUSHUI.ToString();
            this.txtZHONGSHUI.Text = this._YS_STS_SANTONGSGL.I_ZHONGSHUI.ToString();
            this.cbJianGuan.Checked = this._YS_STS_SANTONGSGL.I_PILOUJG == 0;
        }

        private bool ValidateValue()
        {
            //先进行关联数值计算
            this.txtChaZhi.Text = (int.Parse(this.txtSHEJISL.Text) - int.Parse(this.txtPingGuSL.Text)).ToString();
            //判断用户号是否正确
            drc = this._YS_YG_YONGHUXXBLL.SelectYS_YG_YONGHUXXesDynamic("I_JLZT=0 AND S_YONGHUH='"+this.txtYonghuh.Text+"'",null).Rows;
            if (drc.Count==0)
            {
                PageTools.AlertMsg(this,"请输入正确的用户号。");
                return false;
            }
            return true;
        }

        /// <summary>
        /// 添加水平衡记录时保存数据
        /// </summary>
        private void SaveData()
        {
            OperateState operateState;
            //添加
            if (this.Status == GridViewStatus.Add)
            {
                //判断是否有重复
                drc = this._YS_STS_SANTONGSGLBLL.SelectYS_STS_SANTONGSGLsDynamic("I_JLZT=0 AND S_YONGHUH='" + this.txtYonghuh.Text + "'", null).Rows;
                if (drc.Count > 0)
                {
                    PageTools.AlertMsg(this, "已有该记录！");
                    return;
                }                
                operateState = OperateState.Add;
                _YS_STS_SANTONGSGL.ID = 0;
                this._YS_STS_SANTONGSGL.I_JLZT = 0;            
            }
            //修改
            else
            {
                operateState = OperateState.Modify;
                _YS_STS_SANTONGSGL = _YS_STS_SANTONGSGLBLL.SelectYS_STS_SANTONGSGL(this.SANTONGSID, null);               
            }           
            this._YS_STS_SANTONGSGL.S_YONGHUH = this.txtYonghuh.Text.Trim();
            this._YS_STS_SANTONGSGL.S_PINGGUDW = this.txtPINGGUDW.Text;
            this._YS_STS_SANTONGSGL.I_SHEJISL = decimal.Parse(this.txtSHEJISL.Text);
            this._YS_STS_SANTONGSGL.I_PINGGUSL = decimal.Parse(this.txtPingGuSL.Text);
            this._YS_STS_SANTONGSGL.I_CHAZHI = decimal.Parse(this.txtChaZhi.Text);
            this._YS_STS_SANTONGSGL.I_ZHONGSHUI = this.txtZHONGSHUI.Text==""?null:(decimal?)decimal.Parse(this.txtZHONGSHUI.Text);
            this._YS_STS_SANTONGSGL.I_YUSHUI = this.txtYuShui.Text == "" ? null : (decimal?)decimal.Parse(this.txtYuShui.Text);
            this._YS_STS_SANTONGSGL.I_PILOUJG = this.cbJianGuan.Checked?0:1;
            this._YS_STS_SANTONGSGL.D_RIQI = this.txtRQ.Date;

            this._YS_STS_SANTONGSGL.S_CAOZUOR = LoginInfo.CurrentUser.Account;
            this._YS_STS_SANTONGSGL.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();
            _SYS_LOGBLL.Log<YS_STS_SANTONGSGL>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "水平衡管理", operateState, _YS_STS_SANTONGSGL, null);
            if (_YS_STS_SANTONGSGLBLL.InsertOrUpdate(_YS_STS_SANTONGSGL, null))
            {
                
                PageTools.AlertMsg(this, "保存成功！");
                this.Status = GridViewStatus.Init;
                Display();
                LoadData(true);
                //定位记录
                this.SANTONGSID = Web.Class.PageTools.FocusOpreateRecord(this.dtSource, this.gvSANTONGSHI, "S_YONGHUH", _YS_STS_SANTONGSGL.S_YONGHUH, true);
            }
            else
            {
                PageTools.AlertMsg(this, "保存失败！");
            }
        }

        /// <summary>
        /// 查询用户
        /// </summary>
        private void LoadData1(bool alert)
        {
            if (!string.IsNullOrEmpty(this.txtYonghuh.Text))
            {
                DataRowCollection drc = this._YS_YG_YONGHUXXBLL.SelectYS_YG_YONGHUXXesDynamic(string.Format("I_JLZT=0 and S_Yonghuh='{0}' and rownum=1", this.txtYonghuh.Text), "I_LEIXING DESC").Rows;
                if (drc.Count == 1)
                {
                    //记录当前用户号
                    this.txtYonghuh.Text = (drc[0]["S_YONGHUH"]).ToString();
                    this.txtYonghum.Text = drc[0]["S_YONGHUM"].ToString();
                    this.txtDiZhi.Text = drc[0]["S_DIZHI"].ToString();                  
                }
                else
                {
                    if (alert)
                    {
                        PageTools.AlertMsg(this, "该用户不存在,请重新输入！");
                    }
                }
            }
        }

        #endregion
    }
}